﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Security.Cryptography;


namespace TwitterWebPart.Twitter
{
    [ToolboxItemAttribute(false)]
    public class Twitter : WebPart
    {
        /// <textbox property>
        [Category("Twitter Settings"),
        Personalizable(PersonalizationScope.Shared),
        WebBrowsable(true),
        WebDisplayName("Select Maximum No of Tweets"),
        WebDescription("Please Enter No of Tweets")]
        public int MaxTweetsProp { get; set; }

        [Category("Twitter Settings"),
        Personalizable(PersonalizationScope.Shared),
        WebBrowsable(true),
        WebDisplayName("Select Maximum No of Tweets on One Page"),
        WebDescription("Please Enter No of Tweets on One Page")]
        public int TweetsPerPageProp { get; set; }
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/TwitterWebPart/Twitter/TwitterUserControl.ascx";

        /// <textbox control>
        protected override void CreateChildControls()
        {
            TwitterUserControl control = Page.LoadControl(_ascxPath) as TwitterUserControl;
            if (control != null)
                control.WebPart = this;
            Controls.Add(control);

        }
        /// </textbox control>

        public string mystr { get; set; }

        public static string Passphrase { get; set; }
    }
    //Setting propertiees
    public class Tweet          
    {
        public string Id { get; set; }
        public DateTime Published { get; set; }
        public string Link { get; set; }
        public string Title { get; set; }
        public Author Author { get; set; }
      
    }

    public class Author
    {
        public string Name { get; set; }
        public string Uri { get; set; }
        public string ImageSrc { get; set; }



        public object[] TwitterId { get; set; }
    }

    public class TweetStream
    {
        private string m_refreshUri;
        List<Tweet> m_tweets;
        public TweetStream(string queryUri)
        {
            m_refreshUri = queryUri;
            m_tweets = new List<Tweet>();
        }
        public List<Tweet> Tweets
        {
            get
            {
                return m_tweets;
            }
        }
        public void Refresh()
        {
            // TODO - download tweet information from Twitter.com,
            // populate the tweet collection with the new
            // tweet data, and store the new high-watermark
            
                XDocument feed = XDocument.Load(m_refreshUri);                  /////retrieving data from twitter using xml/////


                XNamespace atomNS = "http://www.w3.org/2005/Atom";

                m_tweets = (from tweet in feed.Descendants(atomNS + "entry")
                            select new Tweet
                            {
                                Title = (string)tweet.Element(atomNS + "title"),

                                
                                Published = DateTime.Parse((string)tweet.Element(atomNS + "published")),
                                Id = (string)tweet.Element(atomNS + "id"),
                                Link = tweet.Elements(atomNS + "link")
                                    .Where(link => (string)link.Attribute("rel") == "alternate")
                                    .Select(link => (string)link.Attribute("href"))
                                    .First(),
                                Author = (from author in tweet.Descendants(atomNS + "author")
                                          select new Author
                                          {
                                              Name = (string)author.Element(atomNS + "name"),
                                              Uri = (string)author.Element(atomNS + "uri"),
                                              ImageSrc = (string)author.Element(atomNS + "logo"),
                                              

                                          }).First(),
                            }).ToList<Tweet>();


                m_refreshUri = feed.Descendants(atomNS + "link")
        .Where(link => link.Attribute("rel").Value == "refresh")
        .Select(link => link.Attribute("href").Value)
        .First();


            }
           
        }
    }

